/**
 * Copyright 2009-2010 by The Regents of the University of California
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * you may obtain a copy of the License from
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package edu.ucr.cs.dblab.acestore.base;

import java.io.IOException;
import java.nio.ByteBuffer;

/**
 * @author jarod
 * @created Jan 10, 2011
 */
public interface IPageAdapter {
	
	public void initPage (ByteBuffer page);
	
	public void writeReserved(byte[] head) throws IOException;
	
	public int getReservedLength();
	
	public void reset(ByteBuffer page);
	
	/**
	 * Get the page.
	 * 
	 * @return
	 */
	public ByteBuffer getPage();
	
	/**
	 * Get the number of records in this page.
	 * @return
	 */
	public int getRecordCount();
	
	/**
	 * Get the next page.
	 * 
	 * @return
	 */
	public int getNextPage();
	
	/**
	 * Read a record by the index of the record.
	 * 
	 * @param recordIdx
	 * @return
	 * @throws IOException
	 */
	public ByteBuffer read(int recordIdx) throws IOException;
	
	/**
	 * Insert a record into this page, and return 
	 * whether the insertion is successful.
	 * 
	 * @param record
	 * @return
	 * @throws IOException
	 */
	public boolean insert(byte [] record) throws IOException;
	
	/**
	 * Update a record in this page, and return
	 * whether the insertion is successful.
	 * 
	 * Note that when vari-length record is allowed, 
	 * an update may not be done by overwriting the 
	 * original record directly. This should be considered
	 * in your implementation.
	 * 
	 * @param record
	 * @param recordIdx
	 * @return
	 * @throws IOException
	 */
	public boolean update(byte [] record, int recordIdx) throws IOException;
	
	/**
	 * Delete a record in this page, and return
	 * whether the insertion is successful.
	 * 
	 * The space freed by this operation should be
	 * managed properly.
	 * 
	 * @param recordIdx
	 * @return
	 * @throws IOException
	 */
	public boolean delete(int recordIdx) throws IOException;
}
